package com.edu.app.util;


import com.edu.app.constant.ErrorConstant;
import org.springframework.ui.ModelMap;

import java.lang.reflect.Field;
import java.util.LinkedHashMap;
import java.util.Map;

public class MsgUtil {
    public static ModelMap retMsg(String msg, String obj_name, Object obj){
        ModelMap m = new ModelMap();
        try{
            Class msc_cl = Class.forName("com.edu.app.constant.SysMessage");
            Field em = msc_cl.getField(msg + "_MESSAGE");

            m.put("error_code", 0);
            m.put("error_message", em.get(msc_cl));

            if(obj != null){
                m.put(obj_name, obj);
            }

            return m;
        }catch (Exception e)
        {
            m.put("error_code", ErrorConstant.SERVER_INTERNAL_ERROR);
            m.put("error", ErrorConstant.SERVER_INTERNAL_ERROR);
        }
        return m;
    }

    public static Map<String,Object> retMap(String msg, String obj_name, Object obj){
        Map<String,Object> m = new LinkedHashMap<>();
        try{
            Class msc_cl = Class.forName("com.edu.app.constant.SysMessage");
            Field em = msc_cl.getField(msg + "_MESSAGE");

            m.put("error_code", 0);
            m.put("error_message", em.get(msc_cl));

            if(obj != null){
                m.put(obj_name, obj);
            }

            return m;
        }catch (Exception e)
        {
            m.put("error_code", ErrorConstant.SERVER_INTERNAL_ERROR);
            m.put("error", ErrorConstant.SERVER_INTERNAL_ERROR);
        }
        return m;
    }
}
